package com.app.library.utils

import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.View
import java.util.regex.Matcher
import java.util.regex.Pattern

object TextStringUtils {

    /**
     * 高亮字符串
     *
     * @param text
     * @param highStr
     * @return
     */
    fun highLight(
        text: String,
        highStr: Array<String>,
        callbacks: (Array<() -> Unit>)? = null,
        color: Int = 0,
        size: Float = 1f,
        typeface: Int? = null
    ): SpannableStringBuilder {
        try {
            val creatorBuilder = SpannableStringBuilder()
            var length = 0
            highStr.forEachIndexed { i, high ->
                val index = text.indexOf(high)
                val endIndex = index + high.length
                if (index >= 0 && endIndex < text.length) {
                    creatorBuilder.append(text.substring(length, endIndex))
                    callbacks?.let {
                        creatorBuilder.setSpan(object : ClickableSpan() {
                            override fun onClick(p0: View) {
                                if (callbacks.size > i) {
                                    callbacks[i].invoke()
                                }
                            }
                        }, index, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                    }
                    if (color != 0)
                        creatorBuilder.setSpan(
                            ForegroundColorSpan(color),
                            index,
                            endIndex,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                    if (size != 1f)
                        creatorBuilder.setSpan(
                            RelativeSizeSpan(size),
                            index,
                            endIndex,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                    typeface?.let {
                        creatorBuilder.setSpan(
                            StyleSpan(it), index,
                            endIndex,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                    }
                    length = endIndex
                }

            }
            creatorBuilder.append(text.substring(length, text.length))
            return creatorBuilder
        } catch (e: Exception) {
            e.printStackTrace()
            return SpannableStringBuilder(text)
        }
    }
    fun isUrl(data: String?): Boolean {
        val pattern = Pattern
            .compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$")
        return pattern.matcher(data).matches()
    }

    fun isNumeric(str: String?): Boolean {
        val pattern = Pattern.compile("^(\\-|\\+)?\\d+(\\.\\d+)?\$")
        val isNum: Matcher = pattern.matcher(str)
        return isNum.matches()
    }

    /**
     * 是否为ETH地址
     */
    fun isETHAddress(str: String?): Boolean {
        val pattern = Pattern.compile("0x[a-fA-F0-9]{40}\$")
        val isNum: Matcher = pattern.matcher(str)
        return isNum.matches()
    }
}